﻿namespace ClassyBlog.Web
{
    using System.Collections.Generic;
    using System.Dynamic;
    using System.Linq.Expressions;

    using DomainObjects;
    using Infrastructure;

    public abstract class ViewModel : ICurrentBlogAware, IHasDynamicProperties
    {
        protected ViewModel()
        {
            DynamicProperties = new Dictionary<string, object>();
        }

        public Blog Blog { get; private set; }

        public IEnumerable<string> DynamicPropertyNames
        {
            get { return DynamicProperties.Keys; }
        }

        protected IDictionary<string, object> DynamicProperties
        {
            get; private set;
        }

        public void SetBlog(Blog blog)
        {
            Blog = blog;
        }

        public DynamicMetaObject GetMetaObject(Expression parameter)
        {
            return new DynamicPropertiesMetaObject(parameter, this);
        }

        public object GetDynamicProperty(string name)
        {
            object value;

            return DynamicProperties.TryGetValue(name, out value) ?
                   value :
                   null;
        }

        public void SetDynamicProperty(string name, object value)
        {
            DynamicProperties[name] = value;
        }
    }
}